{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hzy/anaconda3/anaconda/lib/python3.6/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n",
      "/Users/hzy/anaconda3/anaconda/lib/python3.6/importlib/_bootstrap.py:205: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "from xgboost import XGBRegressor\n",
    "import tensorflow as tf\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "import tensorflow.contrib.eager as tfe\n",
    "import functools\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import r2_score\n",
    "from src.utils import mape_score\n",
    "tfe.enable_eager_execution()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('./data/sourceAB_shanghai_mall_processedB.csv', index_col=0)\n",
    "\n",
    "Xa = df.filter(regex='f_a.*', axis=1).values\n",
    "Xb = df.filter(regex='f_b.*', axis=1).values\n",
    "y = np.asarray(df['hotness'].values, dtype=np.float64)\n",
    "y = np.log(df['hotness'].values)\n",
    "\n",
    "Xa = StandardScaler().fit_transform(Xa)\n",
    "Xb = StandardScaler().fit_transform(Xb)\n",
    "Xab = np.concatenate((Xa, Xb), axis=1)\n",
    "# y = (y - y.mean() / y.std())\n",
    "\n",
    "trainA, testA, trainB, testB, trainAB, testAB, trainy, testy \\\n",
    "    = train_test_split(Xa, Xb, Xab, y, test_size=0.2)\n",
    "\n",
    "dataset = tf.data.Dataset.from_tensor_slices({\n",
    "    'feature_a': trainA, \n",
    "    'feature_b': trainB, \n",
    "    'feature_ab': trainAB,\n",
    "    'target': trainy}\n",
    ").shuffle(buffer_size=1000).repeat()\n",
    "\n",
    "dataset_test = tf.data.Dataset.from_tensor_slices({\n",
    "    'feature_a': testA, \n",
    "    'feature_b': testB, \n",
    "    'feature_ab': testAB,\n",
    "    'target': testy}\n",
    ")\n",
    "\n",
    "numpy_dataset = {\n",
    "    'feature_a': trainA, \n",
    "    'feature_b': trainB, \n",
    "    'feature_ab': trainAB,\n",
    "    'target': trainy\n",
    "}\n",
    "\n",
    "numpy_dataset_test = {\n",
    "    'feature_a': testA, \n",
    "    'feature_b': testB, \n",
    "    'feature_ab': testAB,\n",
    "    'target': testy\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "max_step = 3000\n",
    "feature_key = 'feature_ab'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from src.utils import validation_tf\n",
    "from src.tf_models import TypicalNetwork"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use_feature: feature_ab\n",
      "step: 100 /  3000  loss: 1.030725627704858  r2_score: -0.12890580434056553  1 - mape_score: 0.8053781411848142\n",
      "step: 200 /  3000  loss: 0.6740547076669295  r2_score: 0.10490028835928478  1 - mape_score: 0.8241800629046534\n",
      "step: 300 /  3000  loss: 0.5520422206897683  r2_score: 0.19692103747352052  1 - mape_score: 0.8331676662029128\n",
      "step: 400 /  3000  loss: 0.3686394174119213  r2_score: 0.2780732795587414  1 - mape_score: 0.8429928167144595\n",
      "step: 500 /  3000  loss: 0.4102924027958649  r2_score: 0.31438703464361883  1 - mape_score: 0.8447996015077733\n",
      "step: 600 /  3000  loss: 0.4163858991347904  r2_score: 0.3398325733854123  1 - mape_score: 0.8470862458802365\n",
      "step: 700 /  3000  loss: 0.3921824714807507  r2_score: 0.3499242193762382  1 - mape_score: 0.8475572556615634\n",
      "step: 800 /  3000  loss: 0.1711307756699651  r2_score: 0.3844748790792689  1 - mape_score: 0.8528643537547081\n",
      "step: 900 /  3000  loss: 0.4601257527009851  r2_score: 0.3891442025145222  1 - mape_score: 0.8537185818855788\n",
      "step: 1000 /  3000  loss: 0.3020780818901882  r2_score: 0.3999882308162994  1 - mape_score: 0.8552821567223876\n",
      "step: 1100 /  3000  loss: 0.44677123577323086  r2_score: 0.419621932621813  1 - mape_score: 0.8567533503559834\n",
      "step: 1200 /  3000  loss: 0.21404841411836198  r2_score: 0.4282195535737069  1 - mape_score: 0.85974923917713\n",
      "step: 1300 /  3000  loss: 0.35533048599207157  r2_score: 0.4363121870830038  1 - mape_score: 0.8591644493474326\n",
      "step: 1400 /  3000  loss: 0.32424203703379717  r2_score: 0.4301549293559568  1 - mape_score: 0.8583091767353783\n",
      "step: 1500 /  3000  loss: 0.3091641688800209  r2_score: 0.4489035426728549  1 - mape_score: 0.8619567190782924\n",
      "step: 1600 /  3000  loss: 0.30246593551002204  r2_score: 0.4550066922907956  1 - mape_score: 0.86207547695697\n",
      "step: 1700 /  3000  loss: 0.21677584600925917  r2_score: 0.4597280806632724  1 - mape_score: 0.8627989207887182\n",
      "step: 1800 /  3000  loss: 0.27837334668267744  r2_score: 0.4666752576840366  1 - mape_score: 0.8639264768688775\n",
      "step: 1900 /  3000  loss: 0.12233739841175023  r2_score: 0.45837258117796875  1 - mape_score: 0.8628747815389414\n",
      "step: 2000 /  3000  loss: 0.1176280823123861  r2_score: 0.4735073775931654  1 - mape_score: 0.866177185738917\n",
      "step: 2100 /  3000  loss: 0.1720288558022512  r2_score: 0.4752378399362843  1 - mape_score: 0.8656174629596954\n",
      "step: 2200 /  3000  loss: 0.2714681582482389  r2_score: 0.4764967188785394  1 - mape_score: 0.86529587242188\n",
      "step: 2300 /  3000  loss: 0.1508916139316463  r2_score: 0.48759353388436566  1 - mape_score: 0.8672346422340774\n",
      "step: 2400 /  3000  loss: 0.2153276443002411  r2_score: 0.49355653883776196  1 - mape_score: 0.86828270812693\n",
      "step: 2500 /  3000  loss: 0.21556820366394147  r2_score: 0.4931985492610491  1 - mape_score: 0.8677952789964339\n",
      "step: 2600 /  3000  loss: 0.2489496652823467  r2_score: 0.49344967044900967  1 - mape_score: 0.8672281521918014\n",
      "step: 2700 /  3000  loss: 0.27331756844949134  r2_score: 0.4955341525714614  1 - mape_score: 0.8679969185756016\n",
      "step: 2800 /  3000  loss: 0.203111004348782  r2_score: 0.5025715205040318  1 - mape_score: 0.8684221687556095\n",
      "step: 2900 /  3000  loss: 0.20182552294938993  r2_score: 0.5003390660002416  1 - mape_score: 0.867716197850469\n",
      "step: 3000 /  3000  loss: 0.16058193717360925  r2_score: 0.5002467871908494  1 - mape_score: 0.8683848445441027\n",
      "final step: 3000  r2_score: 0.5002467871908494  1 - mape_score: 0.8683848445441027\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(0.50024678719084936, 0.8683848445441027)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = TypicalNetwork(feature_key, hidden_size=40)\n",
    "validation_tf(model, dataset, batch_size, max_step, numpy_dataset_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import Ridge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LinearRegression r2_score 0.400585416729 1 - mape_score -1.13041561842\n"
     ]
    }
   ],
   "source": [
    "rgs = LinearRegression()\n",
    "rgs.fit(numpy_dataset[feature_key], numpy_dataset['target'])\n",
    "predictions = rgs.predict(numpy_dataset_test[feature_key])\n",
    "r2 = r2_score(numpy_dataset_test['target'], predictions)\n",
    "mape = mape_score(numpy_dataset_test['target'], predictions)\n",
    "print(\"LinearRegression\", \"r2_score\", r2, \"1 - mape_score\", 1 - mape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ridge r2_score 0.422106678455 1 - mape_score -1.02737443453\n"
     ]
    }
   ],
   "source": [
    "rgs = Ridge(alpha=0.2)\n",
    "rgs.fit(numpy_dataset[feature_key], numpy_dataset['target'])\n",
    "predictions = rgs.predict(numpy_dataset_test[feature_key])\n",
    "r2 = r2_score(numpy_dataset_test['target'], predictions)\n",
    "mape = mape_score(numpy_dataset_test['target'], predictions)\n",
    "print(\"Ridge\", \"r2_score\", r2, \"1 - mape_score\", 1 - mape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rgs = XGBRegressor()\n",
    "rgs.fit(numpy_dataset[feature_key], numpy_dataset['target'])\n",
    "predictions = rgs.predict(numpy_dataset_test[feature_key])\n",
    "r2, mape = scores(numpy_dataset_test['target'], predictions)\n",
    "print(\"XGBRegressor\", \"r2_score\", r2, \"1 - mape_score\", 1 - mape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 nets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "max_step = 10000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "model = ProposedTwoLayers(use_dropout = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
